#include "lvgl_shares.h"
#include "lvgl_Project.h"
#include "lvgl_SheZhi.h"




_lvgl_shares_data lvgl_shares_data;


void lvgl_shares_event_cb(struct _lv_obj_t * obj, lv_event_t event)
{
	//if (obj == lvgl_shares_data.main_cont)
	{
		shares_debug("cb:%d\r\n", event);
		switch (event)
		{

		case LV_EVENT_DELETE:

			lvgl_shares_closed();
			break;

		case LV_EVENT_PRESSING:


			break;
		case LV_EVENT_PRESSED:


			break;
		case LV_EVENT_DRAG_END:
			shares_debug("拖动后松手\r\n");
			if (lv_obj_get_y(obj) != 0)
			{

				lv_obj_set_y(obj, 0);
			}

			if (lv_obj_get_x(obj) != 0)
			{
				if (lv_obj_get_x(obj) > 100)
				{
					lvgl_shares_close();
				}
				else
				{
					lv_obj_set_x(obj, 0);
				}
			}
			break;
			break;

		case LV_EVENT_RELEASED:

		case LV_EVENT_LONG_PRESSED:

			break;

		default:
			break;

		}

	}
}


void lvgl_shares_create(lv_obj_t * Fu)
{

	if (lvgl_shares_data.main_cont == NULL)
	{

		//----创建容器----//
		lvgl_shares_data.main_cont = lv_cont_create(lv_scr_act(), NULL);
		lv_obj_set_pos(lvgl_shares_data.main_cont, 0, 0);
		lv_obj_set_size(lvgl_shares_data.main_cont, lv_obj_get_width(Fu), lv_obj_get_height(Fu));

		//lv_obj_set_click(lvgl_shares_data.main_cont, true); //启用 / 禁用可点击
		lv_obj_set_drag(lvgl_shares_data.main_cont, true);//启用/禁用对象可拖动
		lv_obj_set_drag_dir(lvgl_shares_data.main_cont, LV_DRAG_DIR_HOR);//设置拖动方向
		//lv_obj_set_drag_throw(lvgl_shares_data.main_cont, false);//启用/禁用拖动后是否有惯性移动

		//lv_obj_set_drag_parent(lvgl_shares_data.main_cont, true); //启用 / 禁用父对象可拖动
		lv_obj_add_style(lvgl_shares_data.main_cont, LV_OBJ_PART_MAIN, &lvgl_WuBianKuang_cont_style);//设置样式
		lv_obj_set_style_local_bg_color(lvgl_shares_data.main_cont, LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_BLUE);

		lv_obj_set_event_cb(lvgl_shares_data.main_cont, lvgl_shares_event_cb);//设置回调函数

		//----日期栏----//
		lvgl_shares_data.bar_cont = lv_cont_create(lvgl_shares_data.main_cont, lvgl_shares_data.main_cont);
		lv_obj_align(lvgl_shares_data.bar_cont, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);
		lv_obj_set_size(lvgl_shares_data.bar_cont, lv_obj_get_width(Fu), 24);

		lvgl_shares_data.date_label = lv_label_create(lvgl_shares_data.bar_cont, NULL);
		lv_label_set_long_mode(lvgl_shares_data.date_label, LV_LABEL_LONG_EXPAND);
		lv_label_set_anim_speed(lvgl_shares_data.date_label, 5);
		lv_label_set_text(lvgl_shares_data.date_label, "2022-09-03");
		lv_obj_align(lvgl_shares_data.date_label, NULL, LV_ALIGN_IN_TOP_LEFT, 3, 0);
		lv_obj_add_style(lvgl_shares_data.date_label, LV_OBJ_PART_MAIN, &lvgl_font12_style);

		lvgl_shares_data.time_label = lv_label_create(lvgl_shares_data.bar_cont, NULL);
		lv_label_set_long_mode(lvgl_shares_data.time_label, LV_LABEL_LONG_EXPAND);
		lv_label_set_anim_speed(lvgl_shares_data.time_label, 5);
		lv_label_set_text(lvgl_shares_data.time_label, "19:58");
		lv_obj_align(lvgl_shares_data.time_label, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
		lv_obj_add_style(lvgl_shares_data.time_label, LV_OBJ_PART_MAIN, &lvgl_font12_style);
		//----日期栏----//


		lvgl_shares_data.item_cont = lv_cont_create(lvgl_shares_data.main_cont, lvgl_shares_data.main_cont);
		lv_obj_set_style_local_bg_color(lvgl_shares_data.item_cont, LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_GREEN);
		lv_obj_set_drag(lvgl_shares_data.item_cont, false);//启用/禁用对象可拖动
		lv_obj_set_drag_parent(lvgl_shares_data.item_cont, false); //启用 / 禁用父对象可拖动
		//lv_obj_set_drag_dir(lvgl_shares_data.item_cont, LV_DRAG_DIR_VER);//设置拖动方向
		lv_obj_set_drag_throw(lvgl_shares_data.item_cont, false);//启用/禁用拖动后是否有惯性移动
		lv_obj_set_pos(lvgl_shares_data.item_cont,0,24);//设置大小
		lv_obj_set_size(lvgl_shares_data.item_cont,lv_obj_get_width(Fu),216);//设置大小



		lvgl_shares_data.item_list = lv_list_create(lvgl_shares_data.item_cont,NULL);
		lv_list_set_scrollbar_mode(lvgl_shares_data.item_list,LV_SCROLLBAR_MODE_DRAG);
		lv_obj_add_style(lvgl_shares_data.item_list, LV_OBJ_PART_MAIN, &lvgl_WuBianKuang_cont_style);//设置样式
		lv_cont_set_fit4(lvgl_shares_data.item_list, LV_FIT_PARENT, LV_FIT_PARENT, LV_FIT_PARENT, LV_FIT_PARENT);



		lv_obj_t * list_btn = NULL;
		for(int i=0;i<10;i++)
		{
			list_btn = lv_list_add_btn(lvgl_shares_data.item_list,NULL,NULL);//添加列表项按钮


			lv_obj_add_style(list_btn, LV_OBJ_PART_MAIN, &lvgl_WuBianKuang_cont_style);//设置样式

			lv_obj_set_style_local_pad_top(list_btn, LV_OBJ_PART_MAIN,LV_STATE_DEFAULT, 5);//内边距上侧尺寸
			lv_obj_set_style_local_pad_bottom(list_btn,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 5);//内边距下侧尺寸
			lv_obj_set_style_local_pad_left(list_btn, LV_OBJ_PART_MAIN,LV_STATE_DEFAULT, 5);//内边距左侧尺寸
			lv_obj_set_style_local_pad_right(list_btn,LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);//内边距右侧尺寸

			sprintf(lvgl_shares_data.items[i].str_code,"%s","300750");
			sprintf(lvgl_shares_data.items[i].str_name,"%s","宁德时代");
			sprintf(lvgl_shares_data.items[i].str_zhangfu,"%s","+1.5%");
			lvgl_shares_data.items[i].mode_zhangfu = SHARES_ZHANGFU_ZHANG;
			lvgl_shares_data.items[i].point.x = 0;
			lvgl_shares_data.items[i].point.y = 0;
			lvgl_shares_item_create(list_btn,&lvgl_shares_data.items[i]);

			//lv_obj_set_event_cb(list_btn,lvgl_shares_event_cb);//给列表项按钮设置事件回调函数
		}



#if 0
		lvgl_shares_data.items[0].main_cont = lv_btn_create(list_btn, NULL);
		lv_obj_set_pos(lvgl_shares_data.items[0].main_cont, lvgl_shares_data.items[0].point.x, lvgl_shares_data.items[0].point.y);
		lv_obj_set_size(lvgl_shares_data.items[0].main_cont, lv_obj_get_width(Fu)-20, 52);

		lv_obj_set_drag_parent(lvgl_shares_data.items[0].main_cont, true); //启用 / 禁用父对象可拖动
		lv_obj_set_click(lvgl_shares_data.items[0].main_cont, true); //启用 / 禁用可点击
		lv_obj_set_drag(lvgl_shares_data.items[0].main_cont, true);//启用/禁用对象可拖动
		lv_obj_set_drag_dir(lvgl_shares_data.items[0].main_cont, LV_DRAG_DIR_VER);//设置拖动方向
		lv_obj_set_drag_throw(lvgl_shares_data.items[0].main_cont, false);//启用/禁用拖动后是否有惯性移动

		sprintf(lvgl_shares_data.items[0].str_code,"%s","300750");
		sprintf(lvgl_shares_data.items[0].str_name,"%s","宁德时代");
		sprintf(lvgl_shares_data.items[0].str_zhangfu,"%s","+1.5%");
		lvgl_shares_data.items[0].mode_zhangfu = SHARES_ZHANGFU_ZHANG;
		lvgl_shares_data.items[0].point.x = 10;
		lvgl_shares_data.items[0].point.y = 39;

		if(lvgl_shares_data.items[0].mode_zhangfu == SHARES_ZHANGFU_ZHANG)
		{
			lv_obj_add_style(list_btn, LV_OBJ_PART_MAIN, &lvgl_shares_item_red_style);
		}else
		if(lvgl_shares_data.items[0].mode_zhangfu == SHARES_ZHANGFU_JIANG)
		{
			lv_obj_add_style(list_btn, LV_OBJ_PART_MAIN, &lvgl_shares_item_green_style);
		}

		//名字
		lvgl_shares_data.items[0].lable_name = lv_label_create(list_btn, NULL);
		lv_label_set_long_mode(lvgl_shares_data.items[0].lable_name, LV_LABEL_LONG_EXPAND);
		lv_label_set_anim_speed(lvgl_shares_data.items[0].lable_name, 5);
		lv_label_set_text(lvgl_shares_data.items[0].lable_name, lvgl_shares_data.items[0].str_name);
		lv_obj_align(lvgl_shares_data.items[0].lable_name, NULL, LV_ALIGN_IN_TOP_LEFT, 5, 0);
		lv_obj_add_style(lvgl_shares_data.items[0].lable_name, LV_OBJ_PART_MAIN, &lvgl_font24_style);

		//代码
		lvgl_shares_data.items[0].lable_code = lv_label_create(list_btn, NULL);
		lv_label_set_long_mode(lvgl_shares_data.items[0].lable_code, LV_LABEL_LONG_EXPAND);
		lv_label_set_anim_speed(lvgl_shares_data.items[0].lable_code, 5);
		lv_label_set_text(lvgl_shares_data.items[0].lable_code, lvgl_shares_data.items[0].str_code);
		lv_obj_align(lvgl_shares_data.items[0].lable_code, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 8, -5);
		lv_obj_add_style(lvgl_shares_data.items[0].lable_code, LV_OBJ_PART_MAIN, &lvgl_font16_style);

		//涨幅
		lvgl_shares_data.items[0].lable_zhangfu = lv_label_create(list_btn, NULL);
		lv_label_set_long_mode(lvgl_shares_data.items[0].lable_zhangfu, LV_LABEL_LONG_EXPAND);
		lv_label_set_anim_speed(lvgl_shares_data.items[0].lable_zhangfu, 5);
		lv_label_set_text(lvgl_shares_data.items[0].lable_zhangfu, lvgl_shares_data.items[0].str_zhangfu);
		lv_obj_align(lvgl_shares_data.items[0].lable_zhangfu, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -20, -5);
		lv_obj_add_style(lvgl_shares_data.items[0].lable_zhangfu, LV_OBJ_PART_MAIN, &lvgl_font16_style);

#endif

		// list_item_btn = lv_list_add_btn(lvgl_shares_data.item_list,NULL,NULL);//添加列表项按钮
		// lv_obj_set_event_cb(list_item_btn,event_handler);//给列表项按钮设置事件回调函数

		// sprintf(lvgl_shares_data.items[0].str_code,"%s","300750");
		// sprintf(lvgl_shares_data.items[0].str_name,"%s","宁德时代");
		// sprintf(lvgl_shares_data.items[0].str_zhangfu,"%s","+1.5%");
		// lvgl_shares_data.items[0].mode_zhangfu = SHARES_ZHANGFU_ZHANG;
		// lvgl_shares_data.items[0].point.x = 10;
		// lvgl_shares_data.items[0].point.y = 39;
		// lvgl_shares_item_create(lvgl_shares_data.item_cont,&lvgl_shares_data.items[0]);

		// sprintf(lvgl_shares_data.items[1].str_code,"%s","002594");
		// sprintf(lvgl_shares_data.items[1].str_name,"%s","比亚迪");
		// sprintf(lvgl_shares_data.items[1].str_zhangfu,"%s","-4.7%");
		// lvgl_shares_data.items[1].mode_zhangfu = SHARES_ZHANGFU_JIANG;
		// lvgl_shares_data.items[1].point.x = 10;
		// lvgl_shares_data.items[1].point.y = 106;
		// lvgl_shares_item_create(lvgl_shares_data.item_cont,&lvgl_shares_data.items[1]);

		// sprintf(lvgl_shares_data.items[2].str_code,"%s","002594");
		// sprintf(lvgl_shares_data.items[2].str_name,"%s","比亚迪");
		// sprintf(lvgl_shares_data.items[2].str_zhangfu,"%s","-4.7%");
		// lvgl_shares_data.items[2].mode_zhangfu = SHARES_ZHANGFU_JIANG;
		// lvgl_shares_data.items[2].point.x = 10;
		// lvgl_shares_data.items[2].point.y = 173;
		// lvgl_shares_item_create(lvgl_shares_data.item_cont,&lvgl_shares_data.items[2]);



		lvgl_task_create(&lvgl_shares_data.task,lvgl_shares_TaskCb, 100, LV_TASK_PRIO_LOWEST, NULL);

		shares_debug("创建窗口\r\n");
	}
	else
	{

		shares_debug("显示窗口\r\n");
	}


	lv_obj_set_drag_throw(lvgl_shares_data.main_cont, false);
	lv_obj_set_pos(lvgl_shares_data.main_cont, 0, 240);
	lvgl_set_obj_show(lvgl_shares_data.main_cont);


	lvgl_shares_anim_Jin();

}


void lvgl_shares_TaskCb(lv_task_t *t)
{
	static int time=0;

	if (lv_obj_get_y(lvgl_shares_data.main_cont) != 0)
	{
		lv_obj_set_drag_throw(lvgl_shares_data.main_cont, true);

	}

	if (lv_obj_get_x(lvgl_shares_data.main_cont) != 0)
	{
		lv_obj_set_drag_throw(lvgl_shares_data.main_cont, false);
	}

}



void lvgl_shares_close(void)
{



	lvgl_desktop_create(lv_scr_act());

	lvgl_task_delete(&lvgl_shares_data.task);

	lvgl_shares_data.point.y = lv_obj_get_y(lvgl_shares_data.main_cont);
	lvgl_shares_data.point.x = lv_obj_get_x(lvgl_shares_data.main_cont);

	lv_obj_clean(lvgl_shares_data.main_cont);

	lv_obj_del(lvgl_shares_data.main_cont);



}


void lvgl_shares_closed(void)
{
	shares_debug("删除窗口\r\n");
	shares_debug("closed\r\n");

	lvgl_shares_data.main_cont = NULL;

}
